package com.zjgsu.nftplatform.controller;

import com.zjgsu.nftplatform.pojo.*;
import com.zjgsu.nftplatform.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author ahy231
 * @date 2022/3/6 10:46
 * @description
 */
@Api(tags = "商品")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    private final GoodsService goodsService;

    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @ApiOperation("广告")
    @GetMapping("/adv")
    public Result advertisement() {
        try {
            return new Result(ResultCode.SUCCESS, goodsService.advertisement());
        } catch (Exception e) {
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("作品高级搜索")
    @PostMapping("/work/research")
    public Result workResearch(@RequestBody GoodsResearch research) {
        try {
            return new Result(ResultCode.SUCCESS, goodsService.workResearch(research));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("交易高级搜索")
    @PostMapping("/deal/research")
    public Result dealResearch(@RequestBody DealResearch research) {
        try {
            return new Result(ResultCode.SUCCESS, goodsService.dealResearch(research));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("nft详情")
    @GetMapping("/goods")
    public Result goodsDetail(@ApiParam("nft id") @RequestParam String nftId) {
        try {
            return new Result(ResultCode.SUCCESS, goodsService.goodsDetail(Long.parseLong(nftId)));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("查看nft评论")
    @GetMapping("/comment")
    public Result commentInfo(@ApiParam("nft id") @RequestParam String nftId) {
        try {
            return new Result(ResultCode.SUCCESS, goodsService.commentInfo(Long.parseLong(nftId)));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("发表评论")
    @PostMapping("/comment")
    public Result comment(@ApiParam("nft id") @RequestParam String nftId,
                          @ApiParam("回复评论id") @RequestParam(required = false) String commentId,
                          @ApiParam("评论内容") @RequestParam String content) {
        try {
            if (commentId != null){
                goodsService.comment(Long.parseLong(nftId), Long.parseLong(commentId), content);
            }else {
                goodsService.comment(Long.parseLong(nftId), null, content);
            }
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("新建商品")
    @PutMapping("/new")
    public Result newGoods(@RequestBody @ApiParam("商品参数") GoodsParameter goodsParameter) {
        try {
            goodsService.newGoods(goodsParameter);
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("上架商品")
    @PostMapping("/sale")
    public Result saleGoods(@RequestParam @ApiParam("商品id") String wareId) {
        try {
            goodsService.saleGoods(Long.parseLong(wareId));
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("拍卖出价")
    @PutMapping("/bidding")
    public Result bidding(@RequestParam @ApiParam("商品id") String wareId, @RequestParam @ApiParam("出价") BigDecimal price) {
        try {
            goodsService.bidding(Long.parseLong(wareId), price);
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("议价")
    @PutMapping("/offer")
    public Result offer(@RequestBody @ApiParam("议价信息") OfferInfo info) {
        try {
            goodsService.offer(info);
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("议价高级搜索")
    @PostMapping("/offer")
    public Result offerSearch(@ApiParam("议价信息") @RequestBody BargainSearch search) {
        try {
            return new Result(ResultCode.SUCCESS, goodsService.offerSearch(search));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("购买商品")
    @PostMapping("/consume")
    public Result consume(@RequestParam @ApiParam("nft id") String nftId) {
        try {
            goodsService.consume(nftId);
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("是否是卖家")
    @GetMapping("/parent/author")
    public Result isParentAuthor(@ApiParam("nft id") @RequestParam String nftId) {
        try {
            return new Result(ResultCode.SUCCESS, goodsService.isParentAuthor(Long.parseLong(nftId)));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
    
    @ApiOperation("修改商品")
    @PostMapping("/modify")
    public Result modify(@ApiParam("商品信息") @RequestBody ModifyGoodsParameter parameter) {
        try {
            goodsService.modify(parameter);
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
    
    @ApiOperation("下架商品")
    @PostMapping("/off")
    public Result off(@ApiParam("商品id") @RequestParam String wareId) {
        try {
            goodsService.off(Long.parseLong(wareId));
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
}
